package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

//import androidx.activity.EdgeToEdge;
//import androidx.appcompat.app.AppCompatActivity;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.OutShelfDetailAdapter;
import com.mflex.WHSmartWhouseAd.model.DoTransTaskInfo;
import com.mflex.WHSmartWhouseAd.model.TaskDetailInfoForDelisModel;
import com.mflex.WHSmartWhouseAd.model.TransTaskInfo;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;
import com.mflex.WHSmartWhouseAd.model.RequestModel;


import java.util.ArrayList;
import java.util.List;


public class OutShelfDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener  {

    EditText MaterialType;
    EditText transType;
    EditText stvOperator;
    EditText PKG;
    ListView txtList;
    Button buttonClear;
    Button buttonSubmit;

    MiniLoadingDialog mMiniLoadingDialog;
    String strTransType;
    String strMaterialType;

    List<TransTaskInfo> list;




    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_out_shelf_detail);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");
        buttonClear = findViewById(R.id.button_clear);
        buttonSubmit= findViewById(R.id.button_save);

        MaterialType= findViewById(R.id.stvMaterialType);
        transType= findViewById(R.id.stvtransType);
        stvOperator= findViewById(R.id.stv_Employee);
        PKG= findViewById(R.id.stvPKG);
        txtList= findViewById(R.id.txtList);

        Intent intent = getIntent();


        strTransType = intent.getStringExtra("transType");
        strMaterialType = intent.getStringExtra("MaterialType");

        // 新增的显示/隐藏逻辑
            LinearLayout llPkg = findViewById(R.id.llPkg);
            if ("原材料".equals(strMaterialType) && "盘点出库".equals(strTransType)) {
                llPkg.setVisibility(View.GONE);
                PKG.setEnabled(false);
            } else {
                llPkg.setVisibility(View.VISIBLE);
                PKG.setEnabled(true);
            }

        transType.setText(strTransType);
        MaterialType.setText(strMaterialType);

        stvOperator.setFocusable(true);
        stvOperator.setFocusableInTouchMode(true);
        stvOperator.requestFocus();
        HideKeyBoardUtil.disableShowSoftInput(stvOperator);
        PKG.setEnabled(false);

        if (strTransType.contains("QMS归还"))
        {
            PKG.setEnabled(true);
        }
        else {
            XHttp.get("api/WHSmartWhousePDA/GetTransTaskData")
                    .params("TransTaskType", strTransType)
                    .params("MaterialType", strMaterialType)
                    .params("PackNo", "")
                    .syncRequest(false)
                    .onMainThread(true)
                    .timeOut(120000)
                    .execute(new SimpleCallBack<List<TransTaskInfo>>() {
                        @Override
                        public void onSuccess(List<TransTaskInfo> response) {
                            PKG.setText("");
                            PKG.requestFocus();
                            list=response;
                            OutShelfDetailAdapter tfOutShelfDetailAdapter=new OutShelfDetailAdapter(OutShelfDetailActivity.this,list);
                            txtList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                            txtList.setAdapter(tfOutShelfDetailAdapter);
                        }

                        @Override
                        public void onError(ApiException e) {
                            mMiniLoadingDialog.dismiss();
                            stvOperator.setText("");
                            stvOperator.requestFocus();
                            MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                            mMiniLoadingDialog.dismiss();
                            XToastUtils.success(e.getMessage(), 1500);
                        }
                    });
        }

        stvOperator.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    HideKeyBoardUtil.hideKeyBoard(OutShelfDetailActivity.this);
                }
            }
        });

        stvOperator.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",stvOperator.getText().toString())) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描员工号", 1500);
                        stvOperator.requestFocus();
                        return false;
                    }

                    XHttp.get("api/WHSmartWhousePDA/CheckActionUser")
                            .params("UserName", stvOperator.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<Boolean>() {
                                @Override
                                public void onSuccess(Boolean response) {
//                                    stvOperator.setText(response);
                                    PKG.setText("");
                                    PKG.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    PKG.setText("");
                                    PKG.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        buttonClear.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(OutShelfDetailActivity.this,OutShelfActivity.class);
                startActivity(intent);
            }
        });

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean flag = false;
                List<String> TaskIds=new ArrayList<>();
                for (TransTaskInfo details : list) {
                    if (details.isCheckbox()) {
                        TaskIds.add(details.getId());
                        flag=true;
                    }
                }
                if (!flag) {
                    MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                    new MaterialDialog.Builder(OutShelfDetailActivity.this)
                            .iconRes(R.drawable.icon_warning)
                            .title(R.string.tip_warning)
                            .content("请选择要提交的任务单号")
                            .positiveText(R.string.lab_submit)
                            .show();

                    return;
                }
               // (A)$$(B)$$(C)116S00022$$(D)10000$$(E)Cyntec$$(F)RR0510S-000-XTFA$$(G)test01$$(H)2431$$(I)111363$$(J)$$(K)123456$$
                if(StringUtils.equals("",stvOperator.getText().toString()))  {
                    mMiniLoadingDialog.dismiss();
                    MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                    XToastUtils.success("请先扫描员工号", 1500);
                    stvOperator.requestFocus();
                    return ;
                }
                mMiniLoadingDialog.show();
                DoTransTaskInfo model=new DoTransTaskInfo(TaskIds,stvOperator.getText().toString());
                RequestModel requestModel = new RequestModel();
                requestModel.setJson(JsonUtil.toJson(model));
                String json = JsonUtil.toJson(requestModel);
                System.out.println(json);
                XHttp.post("api/WHSmartWhousePDA/UpdateTransTask")
                        .upJson(JsonUtil.toJson(model))
//                        .upJson(JsonUtil.toJson(requestModel))
                        .syncRequest(false)
                        .onMainThread(true)
                        .execute(new SimpleCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean response) {
                                    mMiniLoadingDialog.dismiss();
                                    XToastUtils.success("提交成功",3000);
                                    Intent intent=new Intent(OutShelfDetailActivity.this,OutShelfActivity.class);
                                    startActivity(intent);
                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(OutShelfDetailActivity.this)
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });

            }
        });

        PKG.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                        if (PKG.getText().toString() == "") {
                            mMiniLoadingDialog.dismiss();
                            MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                            XToastUtils.success("请描员包号或盘号", 1500);
                            PKG.requestFocus();
                            return false;
                        }
                        XHttp.get("api/WHSmartWhousePDA/GetTransTaskData")
                            .params("TransTaskType", strTransType.trim())
                            .params("MaterialType", strMaterialType.trim())
                            .params("PackNo", PKG.getText().toString().trim())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<List<TransTaskInfo>>() {
                                @Override
                                public void onSuccess(List<TransTaskInfo> response) {
                                    PKG.setText("");
                                    PKG.requestFocus();
                                    list=response;
                                    OutShelfDetailAdapter tfOutShelfDetailAdapter=new OutShelfDetailAdapter(OutShelfDetailActivity.this,list);
                                    txtList.setAdapter(tfOutShelfDetailAdapter);
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    PKG.setText("");
                                    PKG.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });



    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }
}